<?php
namespace app\api\controller;
use think\Controller;
use think\Db;

class House extends Controller
{
    //处理跨域请求
    public function __construct(){
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods:GET, POST, OPTIONS');
    } 
    //根据房源类型查询房源
    public function housetype(){
        $type = $_POST['housetypes'];
        $typeid = Db::name('housetypeinfo')
                ->where('type_content',$type)
                ->find();
        $id = $typeid['id'];
        $res = Db::name('houseinfo')
                ->where('house_type',$id)
                ->select();
        return json($res);
    }
    //查询所有房源
    public function searchhouseinfo(){
        $types = $_POST['housetypes'];
        $result = Db::name('houseinfo')
                ->where('house_type',$types)
                ->select();
        return json($result);
    }
    //根据地区查询房源
    public function searchhousebyaddress(){
        $querytype = $_POST['querytype'];
        $queryval = $_POST['queryval'];
        $result = Db::name('houseinfo')->where($querytype.'','like','%'.$queryval."%")->select();
        return json($result);
    }
    //根据地铁站查询房源
    public function searchhousebymetro(){
        $housemetro = $_GET['housemetro'];
        $result = Db::name('houseinfo')->where('house_intro','like','%'.$housemetro."%")->select();
        return json($result);
    }
    /*全部面积查询所有*/
    public function searchhouseinfoall(){
        $querytype = $_POST['querytype'];
        $queryval = $_POST['queryval'];
        if($queryval==0){
            $result = Db::name('houseinfo')->select();
        }else{
            if($queryval == 1){
                $map['house_floorspace'] = array(array('gt',0),array('lt',100));
            }else if($queryval == 2){
                $map['house_floorspace'] = array(array('gt',100),array('lt',300));
            }else if($queryval == 3){
                $map['house_floorspace'] = array(array('gt',300),array('lt',500));
            }else if($queryval == 4){
                $map['house_floorspace'] = array(array('gt',500),array('lt',700));
            }else if($queryval == 5){
                $map['house_floorspace'] = array(array('gt',1000),array('lt',10000));
            }
            $result = Db::name('houseinfo')->where($map)->select();
        }
        
        return json($result);
    }
    //根据单价查询房源
    public function searchdanjia(){
        $querytype = $_POST['querytype'];
        $queryval = $_POST['queryval'];
        if($queryval==0){
            $result = Db::name('houseinfo')->select();
        }else{
            if($queryval == 1){
                $map['house_unitprice'] = array(array('gt',0),array('lt',40));
            }else if($queryval == 2){
                $map['house_unitprice'] = array(array('gt',40),array('lt',60));
            }else if($queryval == 3){
                $map['house_unitprice'] = array(array('gt',60),array('lt',80));
            }else if($queryval == 4){
                $map['house_unitprice'] = array(array('gt',80),array('lt',120));
            }else if($queryval == 5){
                $map['house_unitprice'] = array(array('gt',120),array('lt',140));
            }else{
                $map['house_unitprice'] = array(array('gt',140),array('lt',200));
            }
            $result = Db::name('houseinfo')->where($map)->select();
        }
        
        return json($result);
    }
    //根据房源总价查询房源
    public function searchzongjia(){
        $querytype = $_POST['querytype'];
        $queryval = $_POST['queryval'];
        if($queryval==0){
            $result = Db::name('houseinfo')->select();
        }else{
            if($queryval == 1){
                $map['house_totalprices'] = array(array('gt',0),array('lt',5000));
            }else if($queryval == 2){
                $map['house_totalprices'] = array(array('gt',5000),array('lt',15000));
            }else if($queryval == 3){
                $map['house_totalprices'] = array(array('gt',15000),array('lt',30000));
            }else if($queryval == 4){
                $map['house_totalprices'] = array(array('gt',30000),array('lt',50000));
            }else if($queryval == 5){
                $map['house_totalprices'] = array(array('gt',50000),array('lt',100000));
            }else{
                $map['house_totalprices'] = array(array('gt',100000),array('lt',1000000));
            }
            $result = Db::name('houseinfo')->where($map)->select();
        }
        
        return json($result);
    }
    
    //得到房源id查询信息
    public function houseId(){
        $house_id = $_GET['house_id'];
        $res = Db::table('houseinfo')
        -> where('id',$house_id)
        -> select();
        
        return json($res);
    }
    
    //添加用户喜欢
    public function collectUser(){
        
    }
}

?>